﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.TES.Entity.SystemEntity;
using ZDSoft.TES.Core;
using NHibernate.Criterion;
using ZDSoft.TES.Service.SystemService;
using ZDSoft.TES.Web.Helpers;

namespace ZDSoft.TES.Web.Controllers
{
    public class RoleController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public void Create(string RoleName, string IsActive, string selectedIds)
        {
            string[] ids = selectedIds.Split(',');
            IList<SystemFunction> selectedSystemFunctions = new List<SystemFunction>();
            foreach (string i in ids)
            {
                if (i != "undefined" && i != "")
                {
                    selectedSystemFunctions.Add(Container.Instance.Resolve<ISystemFunctionService>().FindByID(int.Parse(i.Substring(4))));
                }
            }

            Role role = new Role() { RoleName = RoleName, IsActive = bool.Parse(IsActive), SystemFunctions = selectedSystemFunctions };
            Container.Instance.Resolve<IRoleService>().Create(role);

        }

        public JsonResult GetAllSystemFunctions(int id, int pid, string name, int level)
        {
            IList<SystemFunction> systemFunctions = Container.Instance.Resolve<ISystemFunctionService>().FindAll();
            IList<ZTreeHelper<SystemFunction>.ZTreeNode> tree = new List<ZTreeHelper<SystemFunction>.ZTreeNode>();
            foreach (SystemFunction systemFunction in systemFunctions)
            {
                int tmpID = int.Parse(id.ToString() + systemFunction.ID.ToString());
                ZTreeHelper<SystemFunction>.ZTreeNode node = new ZTreeHelper<SystemFunction>.ZTreeNode();
                node.id = tmpID;
                node.pid = id;
                node.tld = "systemFunction" + systemFunction.ID;
                node.name = systemFunction.SystemFunctionName;
                tree.Add(node);
            }
            JsonResult json = this.Json(tree, JsonRequestBehavior.AllowGet);
            return json;
        }

        public ActionResult Update(int id)
        {
            return View();
        }
        [HttpPost]
        public ActionResult Update(Role role, int id)
        {
            return View();
        }

        public string GetRoleByID(int id)
        {
            Role role = Container.Instance.Resolve<IRoleService>().FindByID(id);
            IList<KeyValuePair<string, InvokeMethod>> list = new List<KeyValuePair<string, InvokeMethod>>();
            //list.Add(new KeyValuePair<string, InvokeMethod>("xx", GetUsers));
            string str = role.ToJsonString<Role>(list);
            return str;
        }

        [HttpPost]
        public void Withdraw(int id)
        {
            Role role = Container.Instance.Resolve<IRoleService>().FindByID(id);
            if (role.IsActive == true)
            {
                role.IsActive = false;
            }
            else
            {
                role.IsActive = true;
            }
            Container.Instance.Resolve<IRoleService>().Update(role);
        }
        public JsonResult GetPaged(int pageIndex, int pageSize, string strSearchProperty,
       string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<Role> list = new List<Role>();
            IList<Role> roles = Container.Instance.Resolve<IRoleService>().GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }
            foreach (Role role in roles)
            {
                Role roleJson = role.ToJaonResult<Role>();
                list.Add(roleJson);
            }
            list.Add(new Role() { ID = pageNum });
            JsonResult result = this.Json(list, JsonRequestBehavior.AllowGet);
            return result;
        }

    }
}
